package com.itheima70.parallaxdemo.ui;

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.ListView;

/**
 * 包含视差特效的ListView
 * @author poplar
 *
 */
public class ParallaxListView extends ListView {

	private ImageView parallaxImage;
	private int drawableHeight;
	private int originalHeight;

	public ParallaxListView(Context context) {
		super(context);
	}
	
	public ParallaxListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}	
	
	public ParallaxListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	public void setParallaxImage(ImageView parallaxImage) {
		this.parallaxImage = parallaxImage;
		// 获取ImageView控件设置的高度
		originalHeight = parallaxImage.getHeight();
		System.out.println("getHeight(): "+originalHeight);
		// 获取ImageView控件测量后的高度
		System.out.println("getMeasuredHeight(): "+parallaxImage.getMeasuredHeight());
		
		// 获取ImageView中图片的大小
		drawableHeight = parallaxImage.getDrawable().getIntrinsicHeight();
	}
	
	// ListView滑动到边缘时候, 此方法会被调用
	// overScroll超出滚动
	@Override
	protected boolean overScrollBy(int deltaX, int deltaY, 
			int scrollX, int scrollY, 
			int scrollRangeX, int scrollRangeY,
			int maxOverScrollX, int maxOverScrollY, 
			boolean isTouchEvent) {
		System.out.println("deltaY: " + deltaY + " scrollY: " + scrollY 
				+ " scrollRangeY: " + scrollRangeY
				+ " maxOverScrollY: " + maxOverScrollY
				+ " isTouchEvent: " + isTouchEvent);
		// deltaY : 竖直方向的瞬时偏移量, 顶部下拉- , 底部上拉+, 速度越快绝对值越大.
		// scrollY : 竖直方向ListView滚动的距离, 顶部- , 底部+
		// scrollRangeY : 竖直方向滚动距离
		// maxOverScrollY : 最大的超出滚动范围
		// isTouchEvent : 是否是手指触摸到顶部/底部. true触摸, false惯性
		
		// 顶部下拉, 手指触摸
		if(deltaY < 0 && isTouchEvent){
			// 将刚刚发生的瞬时偏移量的绝对值 累加 给头布局ImageView
			int newHeight = (int) (parallaxImage.getHeight() + Math.abs(deltaY) / 2.0f);
			
			if(newHeight <= drawableHeight){
				// 让新的高度生效
				setImageViewHeight(newHeight);
			}
			
		}
		
		return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX,
				scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		
		if(ev.getAction() == MotionEvent.ACTION_UP){
			// 松手, 做弹回的动画
			// 从当前高度 弹回 到原始高度
			// 300, -> ,160 originalHeight
			int startHeight = parallaxImage.getHeight();
			
			ValueAnimator animator = ValueAnimator.ofInt(startHeight, originalHeight);
			animator.addUpdateListener(new AnimatorUpdateListener() {
				
				@Override
				public void onAnimationUpdate(ValueAnimator animation) {
					// 500 毫秒内多次调用
					// 0.0 -> 1.0
					System.out.println("animation: " + animation.getAnimatedFraction());
					
					// 模拟出来的中间值
					Object animatedValue = animation.getAnimatedValue();
					System.out.println("animation: " + animatedValue);
					
					// 让新的高度生效
					setImageViewHeight((Integer)animatedValue);
				}
			});
			animator.setInterpolator(new OvershootInterpolator(4));
			animator.setDuration(500);
			animator.start();
			
		}
		
		return super.onTouchEvent(ev);
	}
	

	/**
	 * 设置高度
	 * @param newHeight
	 */
	private void setImageViewHeight(int newHeight) {
		parallaxImage.getLayoutParams().height = newHeight;
		parallaxImage.requestLayout();
	}

}
